﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 27/12/2008
// Time: 21:56
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of City.
	/// </summary>
	public class City : DefaultClass, IClass
	{
		#region private declaration
			private String _state_id;	
			private String _name;
			private String _is_capital;
			private String _area;
			private String _altitude;
			private String _latitude;
			private String _longitude;
			private String _create_date;
			private String _enable;
		#endregion
		
		public City()
		{
			clear();
			gsql.table = "common.city";
			_sequence  = "common.city_city_id_seq";
		}
		
		#region properties
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string stateId 
			{
				get { return _state_id; }
				set { _state_id = value; }
			}
			
			public bool isCapital 
			{
				get { return Convert.ToBoolean(_is_capital); }
				set { _is_capital = Convert.ToString(value); }
			}
			
			public string area 
			{
				get { return _area; }
				set { _area = value; }
			}
			
			public string altitude 
			{
				get { return _altitude; }
				set { _altitude = value; }
			}
			
			public string latitude 
			{
				get { return _latitude; }
				set { _latitude = value; }
			}
			
			public string longitude 
			{
				get { return _longitude; }
				set { _longitude = value; }
			}			
			
			public string createDate 
			{
				get { return _create_date; }
				set { _create_date = value; }
			}
			
			public Boolean enable 
			{
				get { return Convert.ToBoolean(_enable); }
				set { _enable = Convert.ToString(value); }
			}	
		#endregion
	
		public void clear()
		{
			_id          = String.Empty;
			_name        = String.Empty;
			_state_id	 = String.Empty;
			_is_capital	 = Boolean.FalseString;
			_area			 = String.Empty;
			_altitude	 = String.Empty;
			_latitude 	 = String.Empty;
			_longitude 	 = String.Empty;
			_create_date = String.Empty;
			_enable		 = Boolean.TrueString;			
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id,    		 "city_id");
			gsql.setValue(_name,  		 "city_name");
			gsql.setValue( _state_id,	 "city_state_id");	
			gsql.setValue( _is_capital, "city_is_capital");
			gsql.setValue( _area,		 "city_area");
			gsql.setValue( _altitude,	 "city_altitude");
			gsql.setValue( _latitude,	 "city_latitude");
			gsql.setValue( _longitude,	 "city_longitude");			
			//gsql.setValue(_create_date, "city_create_date");
			gsql.setValue(_enable,      "city_enable");
		}
		
		public override void get(DataRow dr)
		{
			_id      	= dr["city_id"].ToString();
			_name       = dr["city_name"].ToString();
			_state_id   = dr["city_state_id"].ToString();
			_is_capital = dr["city_is_capital"].ToString();
			_area		= dr["city_area"].ToString();
			_altitude	= dr["city_altitude"].ToString();
			_latitude	= dr["city_latitude"].ToString();
			_longitude 	= dr["city_longitude"].ToString();
			_create_date= Convert.ToDateTime(dr["city_create_date"]).ToString(common.CONSTANTS.dateFormatBrazilian);
			_enable     = dr["city_enable"].ToString();
		}
	}

	public class Cities : DefaultList, IList
	{
		public Cities()
		{
			gsql.table = "common.city";
		}

		
		/*public List<City> items
		{
			get { return items; }
		}*/
		

		
		public City get(Int32 index)
		{
			return (City)items[index];
		}
		
		public City get()
		{
			return (City)items[index];
		}		
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY city_name";
			
			return mountList();
		}
		
		public bool fillList(string stateId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY city_name";
			
			gsql.conditionalWhere = "city_state_id = "+stateId;
			
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam = gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				City c = new City();
				c.get(gsql.getRow(i));
				items.Add(c);
			}
			return true;
		}	
	}

}
